<?php
include_once 'header.php';

$op = isset($_REQUEST['op']) ? $_REQUEST['op'] : 'list';

// page tpl, must after $op
$page_tpl = rb_get_pagetpl();

// handler
$project_handler =& xoops_getmodulehandler('project');
	
switch($op)
{	
	case 'list':
		include_once XOOPS_ROOT_PATH.'/header.php';
		// css
		rb_add_stylesheet(XOOPS_URL.'/Frameworks/rb/style.css');
		// page title
		$xoopsTpl->assign('xoops_pagetitle', 'My Pjojects');
		// handler
		$project_handler =& xoops_getmodulehandler('project');
		$project_objs = $project_handler->getObjects();
		foreach (array_keys($project_objs) as $p) {
			$project = array();
			$project['pid'] = $project_objs[$p]->getVar('pid');
			$project['name'] = $project_objs[$p]->getVar('name');
			$project['dirname'] = $project_objs[$p]->getVar('dirname');
			$project['version'] = $project_objs[$p]->getVar('version');
			$project['updated'] = formatTimestamp($project_objs[$p]->getVar('updated'), 'Y-m-d H:i');
			$xoopsTpl->append('projects', $project);
		}
		$xoopsTpl->display(RBTOOL_TPL_PATH.'/'.$page_tpl);
		include_once XOOPS_ROOT_PATH.'/footer.php';
		break;
		
	case 'add':
	case 'edit':
		include_once XOOPS_ROOT_PATH.'/header.php';
		rb_add_stylesheet(XOOPS_URL.'/Frameworks/rb/style.css');
		rb_add_stylesheet(RBTOOL_URL.'/css/style.css');
		switch ($op) {
			case 'add':
				$xoopsTpl->assign('xoops_pagetitle', 'New Project');
				// Default Value
				$urname = is_object($xoopsUser) ? $xoopsUser->getVar('uname') : 'your name?';
				$project = array();
				$project['dirname']     = '';
				$project['name']        = '';
				$project['description'] = '';
				$project['version']     = '1.0';
				$project['author']      = $urname;
				$project['credits']     = $urname;
				$project['license']     = 'Mozilla Public License';
				$project['hasmain']     = 1;
				$project['hasadmin']    = 1;
				$project['adminindex']  = 'admin/index.php';
				$project['adminmenu']   = 'admin/menu.php';
				break;
				
			case 'edit':
				$xoopsTpl->assign('xoops_pagetitle', 'Edit Project');
				$pid = isset($_GET['pid']) ? intval($_GET['pid']) : 0;
				if (!$project_handler->isExists($pid)) {
					redirect_header('index.php', 2, 'Project not found!');
					exit();
				}
				$project_obj = $project_handler->get($pid);
				$project = array();
				$project['dirname']     = $project_obj->getVar('dirname');
				$project['name']        = $project_obj->getVar('name');
				$project['description'] = $project_obj->getVar('description');
				$project['version']     = $project_obj->getVar('version');
				$project['author']      = $project_obj->getVar('author');
				$project['credits']     = $project_obj->getVar('credits');
				$project['license']     = $project_obj->getVar('license');
				$project['hasmain']     = $project_obj->getVar('hasmain');
				$project['hasadmin']    = $project_obj->getVar('hasadmin');
				$project['adminindex']  = $project_obj->getVar('adminindex');
				$project['adminmenu']   = $project_obj->getVar('adminmenu');
				break;
		}
		$xoopsTpl->assign('project', $project);
		$xoopsTpl->display(RBTOOL_TPL_PATH.'/rbtool_project_form.html');
		include_once XOOPS_ROOT_PATH.'/footer.php';
		unset($_SESSION['rbtool']['msg']);
		unset($_SESSION['rbtool']['mod_table']);
		break;
		
	case 'insert':
	case 'update':
		$mod_dirname = isset($_POST['mod_dirname']) ? trim($_POST['mod_dirname']) : '';
		if (!$mod_dirname) {
			redirect_header('project.php?op=add', 2, 'module dir name can\'t blank!');
			exit();
		}
		
		$data = array();
		foreach ($_POST as $key => $val) {
			$key = str_replace('mod_', '', $key);
			$data[$key] = $val;
		}
		$data['version']  = floatval($data['version']);
		$data['hasmain']  = isset($data['hasmain'])  ? 1 : 0;
		$data['hasadmin'] = isset($data['hasadmin']) ? 1 : 0;
		$data['updated']  = time();
		
		switch ($op) {
			case 'insert':
				if (!$project_handler->insertRow($data)) {
					redirect_header('index.php', 2, 'Insert error!');
					exit();
				}
				break;
			case 'update':
				if (!$project_handler->updateRow($data)) {
					redirect_header('index.php', 2, 'Update error!');
					exit();
				}
				break;
		}
		redirect_header('index.php', 2, 'Project create success!');
		break;
		
	case 'delete':
		
		if (!is_object($xoopsUser) && !$xoopsUser->isAdmin()) {
			redirect_header('index.php', 2, 'You haven\'t permission!');
			exit();
		}
		
		if (!$project_handler->deleteRow($_GET['pid'])) {
			redirect_header('index.php', 2, 'Delete error!');
		}
		redirect_header('index.php', 2, 'Project delete success!');
		break;
}

?>